<?php
	class NewsDao extends BaseDao {
		
		function getAllNews() {
			parent::getConnect();
			$result = array();
			$rs = mysql_query("select id, author, content, count, publish_date, reference, title, update_date, next_id, prev_id from news");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingNew($record);
				}
			} while ($record=mysql_fetch_object($rs));
			mysql_free_result($rs);
			parent::releaseConnect();
			return $result;
		}
		
		function getNews($page) {
			parent::getConnect();
			$result = array();
			$start = ($page->getPageNo() - 1) * $page->getPageSize();
			$pageSize = $page->getPageSize();
			$rs = mysql_query("select id, author, content, count, publish_date, reference, title, update_date, next_id, prev_id from news limit $start, $pageSize");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingNew($record);
				}
			} while ($record=mysql_fetch_object($rs));
			
			$rs = mysql_query("select count(id) from news");
			$count = mysql_fetch_array($rs);
			$page->setData($result);
			$page->setTotalCount($count[0]);
			
			mysql_free_result($rs);
			parent::releaseConnect();
		}
		
		function getTopNews($topNumber) {
			parent::getConnect();
			$result = array();
			$rs = mysqli_query($this->connect, "select id, author, content, count, publish_date, reference, title, update_date, next_id, prev_id from news limit $topNumber");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingNew($record);
				}
			} while ($record=mysql_fetch_object($rs));
			mysql_free_result($rs);
			parent::releaseConnect();
			return $result;
		}
		
		function getNew($id) {
			parent::getConnect();
			$rs = mysql_query("select id, author, content, count, publish_date, reference, title, update_date, next_id, prev_id from news where id=$id");
			$record = mysql_fetch_object($rs);
			$new = $this->mappingNew($record);
			$new->setNextNew($this->getNewSummary($new->getNextId()));
			$new->setPrevNew($this->getNewSummary($new->getPrevId()));
			mysql_free_result($rs);
			parent::releaseConnect();
			return $new;
		}
		
		private function getNewSummary($id) {
			if ($id == null || $id == 0) {
				return null;
			}
			
			$rs = mysql_query("select id, author, title from news where id=$id");
			$record = mysql_fetch_object($rs);
			if ($record == null) {
				return null;
			}
			$summary = $this->mappingNewSummary($record);
			mysql_free_result($rs);
			return $summary;
		}
		
		function addNew($new) {
			parent::getConnect();
			$title = $new->getTitle(); 
			$author = $new->getAuthor();
			$content = $new->getContent();
			$reference = $new->getReference();
			$rs = mysql_query("select max(id) from news");
			$pid = mysql_fetch_array($rs)[0];
			if ($pid != null) {
				$prevId = $pid;
			} else {
				$prevId = 0;
			}
			$rs = mysql_query("insert into news (author, content, count, publish_date, reference, title, update_date, prev_id) values ('$author', '$content', 0, now(), '$reference', '$title', now(), $prevId)");
			$id = mysql_insert_id();
			$this->setNextNew($prevId, $id);
			$new->setId($id);
// 			mysql_free_result($rs);
			parent::releaseConnect();
		}
		
		function updateNew($new) {
			parent::getConnect();
			$id = $new->getId();
			$title = $new->getTitle();
			$author = $new->getAuthor();
			$content = $new->getContent();
			mysql_query("update news set title='$title', author='$author', content='$content', update_date=now() where id=$id");
			parent::releaseConnect();
		}
		
		function deleteNew($id) {
			parent::getConnect();
			$new = $this->getNew($id);
			if ($new->getPrevId() != null) {
				$this->setNextNew($new->getPrevId(), $new->getNextId());
			}
			if ($new->getNextId() != null) {
				$this->setPrevNew($new->getNextId(), $new->getPrevId());
			}
			$rs = mysql_query("delete from news where id=$id");
			parent::releaseConnect();
		}
		
		private function setPrevNew($id, $prevId) {
			$rs = mysql_query("update news set prev_id=$prevId where id=$id");
		}
		
		private function setNextNew($id, $nextId) {
			$rs = mysql_query("update news set next_id=$nextId where id=$id");
// 			mysql_free_result($rs);
		}
		
		function incrementCount($id) {
			$rs = mysql_query("update news set count=count+1 where id=$id");
// 			parent::releaseConnect();
		}
		
		private function mappingNew($record) {
			$new = new News();
			$new->setId($record->id);
			$new->setAuthor($record->author);
			$new->setContent($record->content);
			$new->setCount($record->count);
			$new->setPublishDate($record->publish_date);
			$new->setReference($record->reference);
			$new->setTitle($record->title);
			$new->setUpdateDate($record->update_date);
			$new->setNextId($record->next_id);
			$new->setPrevId($record->prev_id);
			return $new;
		}
		
		private function mappingNewSummary($record) {
			$new = new News();
			$new->setId($record->id);
			$new->setAuthor($record->author);
			$new->setTitle($record->title);
			return $new;
		}
		
	}
?>